//
//  MTPlaybackController.m
//  MeToo
//
//  Created by 俊松何 on 2018/8/1.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTPlaybackController.h"
#import "MTBackPlayCell.h"
#import "HLiveController.h"

@interface MTPlaybackController ()
@property(nonatomic,assign) int page;
@property(nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation MTPlaybackController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 300;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"MTBackPlayCell" bundle:nil] forCellReuseIdentifier:@"MTBackPlayCellID"];
    self.title = @"回放";
    self.page = 1;
    @weakify(self);
    // 下拉刷刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 1;
        [self getDataIsHeaderRefresh:YES];
    }];
    // 上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.page += 1;
        [self getDataIsHeaderRefresh:NO];
    }];
    [self getDataIsHeaderRefresh:YES];
    [self.tableView emptyViewConfigerBlock:^(FOREmptyAssistantConfiger *configer) {
        configer.emptySubtitle = NSLTLocalizedString(@"还没有直播回放", nil);
        configer.emptySubtitleFont = [UIFont systemFontOfSize:14];
        configer.emptySubtitleColor = UIColorFromRGB(0xcccccc);
        configer.emptyImage = [UIImage imageNamed:@"EmptyBox"];
        configer.shouldDisplay = ^{
            return YES;
        };
    }];
    
}

#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    [[MTLiveApiManager manager] getReplayListWithpage:_page
                                               userId:_userId
                                           completion:^(MTLiveTotalModel *totallive, MTError *mterror) {
        if(mterror == nil)
        {
            if (headerRefresh) {
                self.dataArray = totallive.list;
                [self.tableView reloadData];
            } else {
                [self.dataArray addObjectsFromArray:totallive.list];
                [self.tableView reloadData];
            }
            if (totallive.last) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView.mj_header endRefreshing];
        }else
        {
            if (headerRefresh) {
                [self.tableView.mj_header endRefreshing];
            } else {
                [self.tableView.mj_footer endRefreshing];
            }
        }
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTBackPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTBackPlayCellID" forIndexPath:indexPath];
    cell.liveModel = self.dataArray[indexPath.row];
    @weakify(self);
    cell.deleteBlock = ^(MTLiveModel *model) {
        @strongify(self);
        [MBProgressHUD showProcessHud:nil toView:nil];
        [[MTLiveApiManager manager] deleteReplayWithLiveId:model.Id completion:^(MTError *error) {
            [MBProgressHUD hideHUDForView:nil];
            if(error == nil)
            {
                [MBProgressHUD showSuccess:@"删除成功"];
                [self.dataArray removeObject:model];
                [self.tableView reloadData];
            }else
            {
                [MBProgressHUD showError:error.errorDescription];
            }
        }];
    };
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MTLiveModel *model = self.dataArray[indexPath.row];
    HLiveController *liveVc = [[HLiveController alloc] initWithType:LiveControllertypeReplay];
    liveVc.livemodel = model;
    [self.navigationController pushViewController:liveVc animated:YES];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
